/**
 * 首页逻辑 - 功能导航
 * 提供用户入口、图片选择、功能导航
 */

Page({
  data: {
    userInfo: {},
    processedCount: 0,
    recentImages: [],
    loading: false,
    showActionSheet: false,
    currentImage: null
  },

  /**
   * 页面加载
   */
  onLoad() {
    this.loadUserInfo();
    this.loadRecentImages();
    this.updateProcessedCount();
  },

  /**
   * 页面显示
   */
  onShow() {
    this.updateProcessedCount();
  },

  /**
   * 加载用户信息
   */
  loadUserInfo() {
    const app = getApp();
    this.setData({
      userInfo: app.globalData.userInfo || {},
      processedCount: app.globalData.processedCount
    });
  },

  /**
   * 更新处理数量
   */
  updateProcessedCount() {
    const app = getApp();
    this.setData({
      processedCount: app.globalData.processedCount
    });
  },

  /**
   * 加载最近处理图片
   */
  loadRecentImages() {
    const app = getApp();
    const recentImages = app.globalData.history.slice(0, 5);
    this.setData({ recentImages });
  },

  /**
   * 选择图片
   */
  chooseImage() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const imagePath = res.tempFilePaths[0];
        this.handleSelectedImage(imagePath);
      },
      fail: (err) => {
        wx.showToast({
          title: '选择图片失败',
          icon: 'none'
        });
      }
    });
  },

  /**
   * 拍照
   */
  takePhoto() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['camera'],
      success: (res) => {
        const imagePath = res.tempFilePaths[0];
        this.handleSelectedImage(imagePath);
      }
    });
  },

  /**
   * 批量处理
   */
  batchProcess() {
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        // 跳转到压缩页面，启用批量模式
        wx.navigateTo({
          url: '/pages/compress/compress?batch=true'
        });
      }
    });
  },

  /**
   * 处理选择的图片
   */
  handleSelectedImage(imagePath) {
    // 显示操作菜单
    this.setData({
      currentImage: imagePath,
      showActionSheet: true
    });
  },

  /**
   * 导航到功能页面
   */
  navigateTo(e) {
    const { page } = e.currentTarget.dataset;
    
    if (this.data.currentImage) {
      // 携带图片路径跳转
      wx.navigateTo({
        url: `/pages/${page}/${page}?image=${encodeURIComponent(this.data.currentImage)}`
      });
    } else {
      // 直接跳转
      wx.navigateTo({
        url: `/pages/${page}/${page}`
      });
    }
    
    this.setData({ showActionSheet: false });
  },

  /**
   * 预览图片
   */
  previewImage(e) {
    const { src } = e.currentTarget.dataset;
    wx.previewImage({
      urls: [src],
      current: src
    });
  },

  /**
   * 清空最近记录
   */
  clearRecent() {
    wx.showModal({
      title: '提示',
      content: '确定要清空最近处理记录吗？',
      success: (res) => {
        if (res.confirm) {
          const app = getApp();
          app.clearHistory();
          this.setData({ recentImages: [] });
        }
      }
    });
  },

  /**
   * 隐藏操作菜单
   */
  hideActionSheet() {
    this.setData({ showActionSheet: false });
  }
});